<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
echo "<h2>Заказы</h2>";
$this->table->set_heading('id', 'Клиент', 'Дата', 'Сумма', 'Статус', 'Действия');
$tmpl = array('table_open' => '<table border="0" cellpadding="0" cellspacing="0" class="admin_table">');
$this->table->set_template($tmpl);

$class = 'orders';
if ($this->users_rights_model->check_rights($class, 'block', true)) $canblock = 1;
if ($this->users_rights_model->check_rights($class, 'unblock', true)) $canblock = 1;
if ($this->users_rights_model->check_rights($class, 'edit', true)) $canedit = 1;
if ($this->users_rights_model->check_rights($class, 'delete', true)) $candelete = 1;
if ($this->users_rights_model->check_rights($class, 'add', true)) $canadd = 1;
?>
<? if ($this->input->server('REQUEST_METHOD') != "POST") { ?>
	<div class="filter">
		<a href="/admin/<?=$class?>/?from=<?=$from?>" <?=($this->input->get('status') === false?"class='active'":"")?>>Все</a>
		<a href="/admin/<?=$class?>/?from=<?=$from?>&status=0" <?=($this->input->get('status') === "0"?"class='active'":"")?>>В процессе</a>
		<a href="/admin/<?=$class?>/?from=<?=$from?>&status=1" <?=($this->input->get('status') == 1?"class='active'":"")?>>Новые</a>
		<a href="/admin/<?=$class?>/?from=<?=$from?>&status=2" <?=($this->input->get('status') == 2?"class='active'":"")?>>Обработанные</a>
		<a href="/admin/<?=$class?>/?from=<?=$from?>&status=3" <?=($this->input->get('status') == 3?"class='active'":"")?>>Отмененные</a>
	</div>
<? } ?>
	<div class="filter">
		<form action="/admin/<?=$class?>/" method="post">
			Искать заказ по номеру, дате (формат dd.mm.yyyy), сумме или email покупателя: <input type='text' name='text' value="<?=$this->input->post('text')?>" /> <input type='submit' value='Найти' />
			<? if ($this->input->server('REQUEST_METHOD') == "POST") { ?>
			<a href='/admin/<?=$class?>/'>Очистить фильтр</a>
			<? } ?>
		</form>
	</div>
<?

$parents[0] = 'В процессе';
$parents[1] = 'Новый';
$parents[2] = 'Обработан';
$parents[3] = 'Отменен';

foreach ($pages as $page) {
$oid = $page['order_id'];
$userdata = json_decode($page['order_userdata'], true);
			if ($this->input->post('text') != "") {
			if (preg_match("/\d{2}\.\d{2}\.\d{4}/", $this->input->post('text'), $matches)) {
				$dday = date("d.m.Y H:i:s", $page['order_date']);
				$dday = str_replace($this->input->post('text'), "<b style='color: red;'>".$this->input->post('text')."</b>", $dday);
			} else {
				$s_text = explode(" ",$this->input->post('text'));

				foreach ($s_text as $s_seg) {
					$s_seg = trim($s_seg);
					if ($s_seg != "") {
						$page['order_id'] = preg_replace("/(".$s_seg.")/iu", "<b style='color: red;'>$1</b>", $page['order_id']);
						$page['order_sum'] = preg_replace("/(".$s_seg.")/iu", "<b style='color: red;'>$1</b>", $page['order_sum']);
						$page['order_sum2'] = preg_replace("/(".$s_seg.")/iu", "<b style='color: red;'>$1</b>", $page['order_sum2']);
						$userdata['first_name'] = preg_replace("/(".$s_seg.")/iu", "<b style='color: red;'>$1</b>", $userdata['first_name']);
						$userdata['email'] = preg_replace("/(".$s_seg.")/iu", "<b style='color: red;'>$1</b>", $userdata['email']);
					}
				}
			}
			}
			$dday = date("d.m.Y H:i:s", $page['order_date']);
	$this->table->add_row(
			$page['order_id'],
			"<a href='/admin/clients/edit/".$userdata['id']."/' target='_blank'>".$userdata['first_name']."</a><br />".$userdata['email'],
			"<a title='Просмотр заказа' href='/admin/orders/edit/".$oid."/'>".$dday."</a>",
			$page['order_sum'],
			$parents[$page['order_status']],
			showmeactions(
					$class,
					$oid,
					$page['order_status'],
					$canedit, 0, 0,
					$from
				)
		);
}
echo $this->table->generate();
echo $paging;
echo "<div class='cc'></div>";
?>
